<template>
  <div>
    <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!-- 添加 -->
      <el-button type="primary" @click="dialogFormVisible = true">添加角色</el-button>
      <!-- 添加弹框 -->
      <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="ruleForm.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" prop="roleDesc">
            <el-input v-model="ruleForm.roleDesc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
            <el-button type="primary" @click="addRoles(ruleForm)">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      
      

      <!-- 表格 -->
      <el-table
        border
        :data="tableData"
        style="width: 100%">
        <el-table-column type="expand" width="100px">
          <template slot-scope="scope">
            <div>
              <div class="box" v-if="scope.row.children.length > 0">
                <el-row v-for="item in scope.row.children" :key="item.id">
                  <el-col :span="6">
                    <el-tag closable @close="delrights(scope.row.id,item.id)">{{item.authName}}</el-tag>
                  </el-col> 
                  <el-col :span="18">
                    <el-row v-for="item1 in item.children" :key="item1.id">
                      <el-col :span="6">
                        <el-tag type='success' closable @close="delrights(scope.row.id,item1.id)">{{item1.authName}}</el-tag>
                      </el-col>
                      <el-col :span="18">
                        <el-tag type='warning' closable v-for="item2 in item1.children" :key="item2.id" @close="delrights(scope.row.id,item2.id)">{{item2.authName}}</el-tag>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </div>
              <div v-else>暂无权限</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="#"
          type="index"
          width="40px">
        </el-table-column>
        <el-table-column
          label="角色名称"
          prop="roleName">
        </el-table-column>
        <el-table-column
          label="角色描述"
          prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="EditRoles(scope.row.id)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="delRoles(scope.row.id)">删除</el-button>
            <el-button
              size="mini"
              type="warning"
              @click="emitRoles(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 编辑弹框 -->
      <el-dialog title="编辑角色" :visible.sync="dialogFormVisible1">
        <el-form :model="ruleForm1" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="ruleForm1.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" prop="roleDesc">
            <el-input v-model="ruleForm1.roleDesc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
            <el-button type="primary" @click="updataRoles('ruleForm')">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <!-- 分配角色弹框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible2" width="50%">
          <el-tree
            ref="Tree"
            :data="data"
            show-checkbox
            node-key="id"
            default-expand-all
            :default-checked-keys="checkedArr"
            :props="defaultProps">
          </el-tree>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="updataRoles()">确 定</el-button>
          </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { rolesApi,delrightsApi,delRolesApi,AddRolesApi,editRolesApi,updataRolesApi,rightApi,changeRolesApi  } from "../../http/api";
export default {
  data() {
    return {
      tableData:[],
      dialogFormVisible: false,
      dialogFormVisible1:false,
      dialogVisible2:false,
      ruleForm: {
        roleName: '',
        roleDesc:'',
      },
      ruleForm1:{},
      data:[], 
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      checkedArr:[],
      // 当前角色id
      CurrentRoleId:'',
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
        ],
      }
    }
  },
  created(){
    this.getRoleslist()
  },
  methods: {
    // 渲染角色
    async getRoleslist(){
      const res = await rolesApi()
      console.log(res);
      this.tableData = res
    },
    // 添加
    addRoles() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) {
          return false
        }
        const res = await AddRolesApi(this.ruleForm)
        console.log(res);
        this.getRoleslist()
        this.dialogFormVisible = false
      });
    },
    // 删除权限标签
    async delrights(roleId,rightId){
      const res = await delrightsApi(roleId,rightId)
      console.log(res);
      this.getRoleslist()
    },
    // 删除角色
    delRoles(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRolesApi(id).then(res=>{
          console.log(res);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getRoleslist()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    // 编辑
    async EditRoles(id){
      this.dialogFormVisible1 = true
      const res = await editRolesApi(id)
      console.log(res);
      this.ruleForm1 = res
    },
    // 编辑确定
    updataRoles(id){
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) {
          return false
        }
        const {roleId,roleName,roleDesc} = this.ruleForm1
        console.log(this.ruleForm1);
        const res = await updataRolesApi(roleId,{roleName:roleName,roleDesc:roleDesc})
        console.log(res);
        this.dialogFormVisible1 = false
        this.getRoleslist()
      })
    },
    // 分配
    async emitRoles(row){
      this.CurrentRoleId = row.id
      const res = await rightApi('tree')
      console.log(res);
      this.data=res

      this.getCurrentRoleRightsArr(row,this.checkedArr)
      this.dialogVisible2 = true
    },
    // 递归调用
    getCurrentRoleRightsArr(row,checkedArr){
      if(!row.children){
        return checkedArr.push(row.id)
      }
      row.children.forEach((item)=>{
        this.getCurrentRoleRightsArr(item,checkedArr)
      })
    },
    // 分配权限
    async updataRoles(){
      console.log(this.CurrentRoleId);
      const allChecked = this.$refs.Tree.getCheckedKeys()
      const halChecked = this.$refs.Tree.getHalfCheckedNodes()
      
      const resutCheck = [...allChecked,...halChecked].join(',')
      const res = await changeRolesApi(this.CurrentRoleId,resutCheck)
      console.log(res);
      this.getRoleslist()
      this.dialogVisible2 = false
    },
    // 添加弹窗重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
}
</script>

<style>
  .box-card{
    margin: 10px 0;
  }
  .el-table{
    margin: 10px 0;
  }
  .box{
    padding: 20px;
  }
  .box .el-tag{
    margin: 10px;
  }
</style>